package de.fuberlin.processing.who.cancer.utils.graphics;

import processing.core.PApplet;


public class CurveRing {

	private PApplet p;


	private CurveRing( PApplet papplet) {
		this.p = papplet;
	}

	
	private void arc( int x, int y, float outerRadius,
			float innerRadius, int segmentCount) {
		if ( innerRadius <= 0.0f) {
			p.pushStyle();
			p.ellipseMode( p.RADIUS);
			p.ellipse( x, y, outerRadius, outerRadius);
			p.popStyle();
		} else {
			p.pushStyle();
			p.noStroke();

			float delta = ( 1.0f / (float) segmentCount) * p.TWO_PI;
			p.beginShape( p.POLYGON);

			// outer circle
			p.curveVertex( x + outerRadius * p.cos( 0),
					y + outerRadius * p.sin( 0));
			for ( int i = 0; i <= segmentCount; i++) {
				p.curveVertex( x + outerRadius * p.cos( i * delta), y
						+ outerRadius * p.sin( i * delta));
			}
			p.curveVertex(
					x + outerRadius * p.cos( ( segmentCount + 1) * delta), y
							+ outerRadius * p.sin( ( segmentCount + 1) * delta));

			p.vertex( x + outerRadius * p.cos( 0),
					y + outerRadius * p.sin( 0));
			p.vertex( x + innerRadius * p.cos( ( segmentCount + 1) * delta), y
					+ innerRadius * p.sin( ( segmentCount + 1) * delta));

			// inner circle
			p.curveVertex(
					x + innerRadius * p.cos( ( segmentCount + 1) * delta), y
							+ innerRadius * p.sin( ( segmentCount + 1) * delta));
			for ( int i = segmentCount + 1; i >= 0; i--) {
				p.curveVertex( x + innerRadius * p.cos( i * delta), y
						+ innerRadius * p.sin( i * delta));
			}
			p.curveVertex( x + innerRadius * p.cos( 0),
					y + innerRadius * p.sin( 0));
			p.endShape();
			p.popStyle();
			p.pushStyle();
			p.noFill();
			p.ellipseMode( p.RADIUS);
			p.ellipse( x, y, outerRadius, outerRadius);
			p.ellipse( x, y, innerRadius, innerRadius);
			p.popStyle();
		}
	}
	
	public static void arc( PApplet p, int x, int y, float outerRadius,
			float innerRadius, int segmentCount) {
		CurveRing r = new CurveRing( p);
		r.arc( x, y, outerRadius, innerRadius, segmentCount);
	}

}
